package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.adapter.TravelAddressPickAdapter;
import com.wnhz.work.yz.bean.XinChengBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zxb
 * @date 2016/12/19
 * @describe 行程地点选择
 */
public class TravelAddressPickActivity extends BaseActivity implements View.OnClickListener {


    private ListView lv_driver_pick;
    private TravelAddressPickAdapter pickAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_driver_pick);
        onUpload();
        initData();

    }

    private List<XinChengBean.InfoEntity> info1 = new ArrayList<>();

    private void onUpload() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        showDialog();
      /*  Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----" + key, ":" + value + "\n");
            }
        }*/
        XUtil.Post(Url.INFORMATION_PLACE_INFO, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    Log.e("----", "onSuccess:= " + info);
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        XinChengBean bean = gson.fromJson(result, XinChengBean.class);
                        info1 = bean.getInfo();
                        initTitle();
                        initView();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });


    }

    private void initData() {

    }


    private TextView title;

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("行程地点");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
    }


    private void initView() {

        lv_driver_pick = (ListView) findViewById(R.id.lv_driver_pick);

        pickAdapter = new TravelAddressPickAdapter(this, info1, this);
        lv_driver_pick.setAdapter(pickAdapter);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:

                finish();
                break;


            case R.id.rl_item_travel_address:

                int index = (Integer) v.getTag();

                String getName = info1.get(index).getPlace();

//                MyToast("选择了===" + getName);

                int size = info1.size();
                for (int i = 0; i < size; i++) {
                    if (i == index) {
                        info1.get(i).setShow(true);
                    } else {
                        info1.get(i).setShow(false);
                    }
                }
                pickAdapter.notifyDataSetChanged();

                intent = new Intent();
                intent.putExtra("address", getName);
                intent.putExtra("place",info1.get(index).getPlace_id());
                setResult(RESULT_OK, intent);
                finish();

                break;
            default:
                break;
        }

    }
}
